<template>
  <slide>
    <div class="user-center-wrapper">
      <div class="top">
        <span
          class="icon-back back"
          @click="back"
          ></span>
        <div class="switches-wrapper">
          <switches
            :switches="switches"
            :currentIndex="currentIndex"
            @switch="switchItem"
            ></switches>
        </div>
      </div>
      <!-- 随机播放按钮 -->
      <div
        class="shuffle-play-btn"
        @click="random"
        >
        <span class="icon-play-mini"></span>
        <span class="desc">随机播放全部</span>
      </div>
      <!-- 内容区 -->
      <div class="list-wrapper" ref="listWrap">
        <scroll
          class="scroll"
          v-show="currentIndex === 0"
          :data="favoriteList"
          ref="favoriteList"
          >
          <song-list
            :songs="favoriteList"
            @select="selectSong"
            ></song-list>
        </scroll>
        <scroll
          class="scroll"
          v-show="currentIndex === 1"
          :data="playHistory"
          ref="historyList"
          >
          <song-list
            :songs="playHistory"
            @select="selectSong"
            ></song-list>
        </scroll>
      </div>
      <!-- no-result -->
      <div class="no-result-wrapper" v-show="noResult">
        <no-result :title="noResultDesc"></no-result>
      </div>
    </div>
  </slide>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import Slide from 'base/slide/slide'
import Switches from 'base/switches/switches'
import Scroll from 'base/scroll/scroll'
import SongList from 'base/song-list/song-list'
import NoResult from 'base/no-result/no-result'
import { playlistMixin } from 'common/js/mixin'

export default {
  name: 'UserCenter',
  mixins: [playlistMixin],
  components: {
    Switches,
    Scroll,
    SongList,
    Slide,
    NoResult
  },
  data () {
    return {
      switches: ['我喜欢的', '最近听的'],
      currentIndex: 0,
      timer: null
    }
  },
  computed: {
    noResult () {
      if (this.currentIndex === 0) {
        return !this.favoriteList.length > 0
      } else {
        return !this.playHistory.length > 0
      }
    },
    noResultDesc () {
      if (this.currentIndex === 0) {
        return '暂无收藏歌曲'
      } else {
        return '暂无播放历史'
      }
    },
    ...mapGetters([
      'favoriteList',
      'playHistory'
    ])
  },
  methods: {
    back () {
      this.$router.back()
    },
    switchItem (index) {
      this.currentIndex = index
      this.refreshScroll()
    },
    selectSong (song, index) {
      this.insertSong(song)
    },
    refreshScroll () {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        if (this.currentIndex === 0) {
          this.$refs.favoriteList.refresh()
        } else {
          this.$refs.historyList.refresh()
        }
      }, 20)
    },
    random () {
      let list = this.currentIndex === 0 ? this.favoriteList : this.playHistory
      if (!list.length > 0) {
        return
      }
      this.randomPlay({ list })
    },
    handlePlaylist (playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.listWrap.style.bottom = bottom
      this.$refs.favoriteList.refresh()
      this.$refs.historyList.refresh()
    },
    ...mapActions([
      'insertSong',
      'randomPlay'
    ])
  }
}
</script>

<style lang="stylus" scoped>
  @import '~common/stylus/variable.styl'
  .user-center-wrapper {
    position: fixed
    top: 0
    right: 0
    bottom: 0
    left: 0
    background: $color-background
    .top {
      display: flex
      flex-direction: column
      justify-content: center
      height: 44px
      line-height: 44px
      color: $color-theme
      .back {
        position: absolute
        left: 0
        line-height: 44px
        padding-left: 10px
        font-size: $font-size-large
      }
    }
    .shuffle-play-btn {
      position: absolute
      left: 50%
      top: 72px
      transform: translateX(-50%)
      box-sizing: border-box
      width: 120px
      height: 28px
      line-height: 28px
      border: 1px solid $color-text-l
      border-radius: 14px
      text-align: center
      color: $color-text-l
      .desc {
        margin-left: 4px
        font-size: $font-size-small-s
        vertical-align: 2px
      }
    }
    .list-wrapper {
      position: absolute
      top: 120px
      right: 0
      bottom: 0
      left: 0
      overflow: hidden
      .scroll {
        height: 100%
      }
    }
  }
</style>
